package com.main.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.web.util.GenericoUtil;


public class GenericoMailSender{

	public static void enviarMensaje(String clave, String usuario, String destinatario, String proceso, String nombre) {
		Properties properties = GenericoUtil.getPropertiesFile(GenericoUtil.GEN_WEB_PROPERTIES);
		final String password = properties.getProperty("mail.password");
		final String username = properties.getProperty("mail.username");
		
		Properties props = new Properties();
		props.put("mail.smtp.host", properties.getProperty("mail.host").toString());
		props.put("mail.smtp.socketFactory.port", properties.getProperty("mail.port").toString());
		props.put("mail.smtp.socketFactory.class", properties.getProperty("mail.socketFactory.class").toString());
		props.put("mail.smtp.auth", properties.getProperty("mail.auth").toString());
		props.put("mail.smtp.port", properties.getProperty("mail.port").toString());
 
		Session session = Session.getDefaultInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(username, password);
					
				}
			});
 
		try {
 
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(properties.getProperty("mail.from").toString()));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(destinatario));
			
			
			
			if (proceso.equals("restablecerClave")) {
				message.setSubject("Solicitud de Reestablecimiento de Clave");
				message.setText("Apreciable Sr(a)," + nombre +
						"\n\n Su usuario es: " + usuario +
						"\n\n Su clave es:   " + clave);
			}
			
 
			Transport.send(message);
 
			System.out.println("Done");
 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
	
}
